/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InterfazConsultarCalificaciones.java
 *
 * Created on 18/10/2011, 01:30:54 PM
 */

package presentacion;

import Paneles.PanelGeneral;
import business.AsignaturaBO;
import business.EstudianteBO;
import business.EstudianteLogroBO;
import business.GradoBo;
import business.LogroBO;
import business.ProfesorBo;
import dao.AsignaturaDao;
import dao.ProfesorDao;
import dominio.Asignatura;
import dominio.Estudiante;
import dominio.EstudianteLogro;
import dominio.Grado;
import dominio.Logro;
import java.awt.BorderLayout;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.swing.ImageIcon;

/**
 *
 * @author viviana
 */
public class InterfazConsultarCalificaciones extends javax.swing.JFrame {

    private ImageIcon icono;
    private Grado grado;
    private Asignatura asignatura;
    /** Creates new form InterfazConsultarCalificaciones */
    public InterfazConsultarCalificaciones() {
        initComponents();

        PanelGeneral panel=new PanelGeneral();
        this.add(panel,BorderLayout.SOUTH);
        icono=new ImageIcon(getClass().getResource("/Imagenes/icon.gif"));
        this.setIconImage(icono.getImage());
        inicializarComboBox();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TLogro = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        TEstudiante = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        cBGrado = new javax.swing.JComboBox();
        CBNombreAsignatura = new javax.swing.JComboBox();

        setTitle("Consultar Notas");
        setResizable(false);

        jLabel1.setText("Grado:");

        jLabel2.setText("Asignatura:");

        TLogro.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Nro. del Logro", "Descripción del Logro"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(TLogro);

        TEstudiante.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Nombre del Estudiante", "Nro. del Logro", "Calificación"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(TEstudiante);

        jButton1.setText("Aceptar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        cBGrado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccione" }));
        cBGrado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cBGradoActionPerformed(evt);
            }
        });

        CBNombreAsignatura.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccione" }));
        CBNombreAsignatura.setEnabled(false);
        CBNombreAsignatura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBNombreAsignaturaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 701, Short.MAX_VALUE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 701, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(CBNombreAsignatura, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cBGrado, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(190, 190, 190)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cBGrado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(CBNombreAsignatura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 402, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void cBGradoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cBGradoActionPerformed
        if (cBGrado.getSelectedIndex() > 0){
            AsignaturaBO asignaturaBO = new AsignaturaBO();
            GradoBo gradoBo = new GradoBo();
            grado = gradoBo.encontrarGrado(Integer.parseInt((String)cBGrado.getSelectedItem()));
            List asignaturas = asignaturaBO.consultarAsignaturasGrado(grado.getIdGrado());
            if (asignaturas.size() > 0){
                CBNombreAsignatura.setEnabled(true);
                for (Iterator it = asignaturas.iterator(); it.hasNext();) {
                    Asignatura asignatura1 = (Asignatura) it.next();
                    CBNombreAsignatura.addItem(asignatura1.getNombreAsignatura());
                }
            }
        }
        else{
            CBNombreAsignatura.setSelectedIndex(0);
            CBNombreAsignatura.setEnabled(false);
        }
    }//GEN-LAST:event_cBGradoActionPerformed

    private void CBNombreAsignaturaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBNombreAsignaturaActionPerformed
        if (CBNombreAsignatura.getSelectedIndex() > 0){
            actualizarTablas();
        }
    }//GEN-LAST:event_CBNombreAsignaturaActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new InterfazConsultarCalificaciones().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox CBNombreAsignatura;
    private javax.swing.JTable TEstudiante;
    private javax.swing.JTable TLogro;
    private javax.swing.JComboBox cBGrado;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables

    private void inicializarComboBox() {
        GradoBo gradoBo = new GradoBo();
        List grados = gradoBo.consultarGrados();
        for (Iterator it = grados.iterator(); it.hasNext();) {
            Grado grado1 = (Grado) it.next();
            cBGrado.addItem(String.valueOf(grado1.getIdGrado()));
        }
    }

    private void borrarTablas() {
        int fila = 0;
        while (TEstudiante.getValueAt(fila, 0) != null){
            TEstudiante.setValueAt(null, fila, 0);
            TEstudiante.setValueAt(null, fila, 1);
            TEstudiante.setValueAt(null, fila, 2);
            fila++;
        }
        fila = 0;
        while (TLogro.getValueAt(fila, 0) != null){
            TLogro.setValueAt(null, fila, 0);
            TLogro.setValueAt(null, fila, 1);
            fila++;
        }
    }

    private void actualizarTablas() {
        borrarTablas();
        EstudianteLogroBO estudianteLogroBO = new EstudianteLogroBO();
        List estudLogs = estudianteLogroBO.consultarListaEstLogros(asignatura.getIdAsignatura());
        int fila = 0;
        EstudianteBO estudianteBO = new EstudianteBO();
        LogroBO logroBO = new LogroBO();
        List logros = logroBO.consultarListaLogros(asignatura.getIdAsignatura(), 0000000);
        for (Iterator it = logros.iterator(); it.hasNext();) {
            Logro logro = (Logro) it.next();
            TLogro.setValueAt(String.valueOf(logro.getIdlogro()), fila, 0);
            TLogro.setValueAt(logro.getDescripcion(), fila, 1);
            fila++;
        }
        fila = 0;
        for (Iterator it = estudLogs.iterator(); it.hasNext();) {
            EstudianteLogro estudianteLogro = (EstudianteLogro) it.next();
            Estudiante estudiante = estudianteBO.obtenerEstudiantePorDocumento(estudianteLogro.getIdestudiante());
            TEstudiante.setValueAt(estudiante.getNombreEst(), fila, 0);
            TEstudiante.setValueAt(String.valueOf(estudianteLogro.getIdlogro()), fila, 1);
            TEstudiante.setValueAt(estudianteLogro.getCalificacionlogro(), fila, 2);
            fila++;
        }
    }
}